﻿#include "progressinfo.h"

ProgressInfo::ProgressInfo(QObject *parent)
    : QBaseEntity(parent)
{
    clear();
}

ProgressInfo::~ProgressInfo()
{
}

QJsonObject ProgressInfo::toJsonObject(PropertiesType type, QStringList propertyList) const
{
    QJsonObject jsonObject;
    const QMetaObject *metaObject = this->metaObject();
    for (int i = 0; i < metaObject->propertyCount(); ++i)
    {
        QMetaProperty metaProperty = metaObject->property(i);
        if (metaProperty.isReadable())
        {
            QVariant value = metaProperty.read(this);
            if (value.isValid())
            {
                QString propertyName = metaProperty.name();
                // 忽略 objectName 属性
                if (propertyName == "objectName")
                {
                    continue;
                }
                if (metaProperty.userType() == qMetaTypeId<Progress>())
                {
                    // 处理 Progress 类型的属性
                    Progress object = value.value<Progress>();
                    jsonObject[propertyName] = object.toJsonObject();
                }
                else if (propertyName == "cpuUsageList")
                {
                    QJsonArray jsonArray;
                    QList<float> values = value.value<QList<float>>();
                    for (float val : values)
                    {
                        jsonArray.append(val);
                    }
                    jsonObject[propertyName] = jsonArray;
                }
                else
                {
                    jsonObject[propertyName] = qvariantToJsonValue(value);
                }
            }
        }
    }
    return jsonObject;
}

void ProgressInfo::fromJsonObject(const QJsonObject &jsonObject)
{
    const QMetaObject *metaObject = this->metaObject();
    for (int i = 0; i < metaObject->propertyCount(); ++i)
    {
        QMetaProperty metaProperty = metaObject->property(i);
        if (metaProperty.isWritable())
        {
            QString propertyName = metaProperty.name();
            if (jsonObject.contains(propertyName))
            {
                QJsonValue jsonValue = jsonObject[propertyName];
                if (jsonValue.isNull())
                {
                    continue;
                }
                if (metaProperty.userType() == qMetaTypeId<Progress>())
                {
                    Progress object;
                    object.fromJsonObject(jsonValue.toObject());
                    metaProperty.write(this, QVariant::fromValue(object));
                }
                else
                {
                    QVariant value = qjsonValueToVariant(jsonValue, metaProperty.type());
                    metaProperty.write(this, value);
                }
            }
        }
    }
}

Progress ProgressInfo::getProgress() const
{
    return progress;
}

void ProgressInfo::setProgress(const Progress &value)
{
    progress = value;
}

bool ProgressInfo::getEnabled() const
{
    return enabled;
}

void ProgressInfo::setEnabled(bool value)
{
    enabled = value;
}

uint64_t ProgressInfo::getRunTime() const
{
    return runTime;
}

void ProgressInfo::setRunTime(const uint64_t &value)
{
    runTime = value;
}

float ProgressInfo::getCpuUsage() const
{
    return cpuUsage;
}

void ProgressInfo::setCpuUsage(float value)
{
    cpuUsage = value;
    cpuUsageList.append(value);
    while (cpuUsageList.size() > 5)
    {
        cpuUsageList.takeFirst();
    }
}

QString ProgressInfo::getStateMsg() const
{
    return stateMsg;
}

void ProgressInfo::setStateMsg(const QString &value)
{
    stateMsg = value;
}

uint64_t ProgressInfo::getMemory() const
{
    return memory;
}

void ProgressInfo::setMemory(const uint64_t &value)
{
    memory = value;
}

uint64_t ProgressInfo::getVirtualMemory() const
{
    return virtualMemory;
}

void ProgressInfo::setVirtualMemory(const uint64_t &value)
{
    virtualMemory = value;
}

uint64_t ProgressInfo::getIoRead() const
{
    return ioRead;
}

void ProgressInfo::setIoRead(const uint64_t &value)
{
    ioRead = value;
}

uint64_t ProgressInfo::getIoWrite() const
{
    return ioWrite;
}

void ProgressInfo::setIoWrite(const uint64_t &value)
{
    ioWrite = value;
}

QList<float> ProgressInfo::getCpuUsageList() const
{
    return cpuUsageList;
}

void ProgressInfo::setCpuUsageList(const QList<float> &value)
{
    cpuUsageList = value;
}
